package com.nny.alarm.edit;

import com.nny.alarm.edit.adapter.CycleAdapter;
import com.nny.alarm.widget.DialView;
import com.nny.alarm.widget.TimeView;
import com.uxwine.alarm.TimeCycle2;
import com.uxwine.widget.ActionDialog;

public class CycleEditor implements IAlarmEditor, ActionDialog.OnActionListener
{
	AlarmEditor mEditor;
	CycleAdapter mAdpater;
	long mlFlagHour;
	long mlFlagWeek;
	
	public CycleEditor(AlarmEditor e)
	{
		mEditor = e;
		mlFlagHour = TimeCycle2.FLAG_HOUR_DEFAULT;
		mlFlagWeek = TimeCycle2.FLAG_NONE;
	}
	
	@Override
	public void edit()
	{
		final TimeView dttv = mEditor.mDigital;
		final DialView dtv = mEditor.mDial;
		
		initCycleRecord(mEditor.mAlarm.cycle);
		dttv.update(mEditor.mAlarm);
		
		mAdpater = new CycleAdapter(mEditor.mCtx);
		mAdpater.setPosition(getCyclePosition(mEditor.mAlarm.cycle));
		dtv.setAdapter(mAdpater);
		mAdpater.notifyDataSetChanged();
	}

	@Override
	public boolean next()
	{
		mEditor.step(AlarmEditor.STEP_TIME);
		return true;
	}

	@Override
	public void onMarked(int index)
	{
		mEditor.mAlarm.cycle.setCycle(getCycleByPosition(index));
		if(index == 6)
		{
			selectWeekCycle();
		} 
		else if(index == 7)
		{
			selectHourCycle();
		}
		else
		{
			checkCycleFlags(mEditor.mAlarm.cycle);
		}
		mEditor.mDigital.update(mEditor.mAlarm);
	}

	@Override
	public void onMarkChange(int index)
	{
		
	}
	
	private void selectWeekCycle()
	{
		CycleWeekDialog dlg = new CycleWeekDialog(mEditor, this);
		dlg.setOnActionListener(this);
		dlg.show();
	}
	
	private void selectHourCycle()
	{
		CycleHourDialog dlg= new CycleHourDialog(mEditor, this);
		dlg.setOnActionListener(this);
		dlg.show();
	}
	
	public static int getCyclePosition(TimeCycle2 c)
	{
		return c.getCycle();
	}
	
	public int getCycleByPosition(int pos)
	{
		return pos;
	}

	@Override
	public void over()
	{
	}

	@Override
	public void reset()
	{
		checkCycleFlags(mEditor.mAlarm.cycle);
		mlFlagHour = TimeCycle2.FLAG_HOUR_DEFAULT;
		mlFlagWeek = TimeCycle2.FLAG_NONE;	
	}

	@Override
	public void onExit(int code)
	{		
		final int cycle = mEditor.mAlarm.cycle.getCycle();
		if(cycle == TimeCycle2.TYPE_HOUR)
		{
			
		}
		else if(cycle == TimeCycle2.TYPE_WEEK)
		{
			checkWeekSet();
		}
	}
	
	protected void checkWeekSet()
	{
		int pos = 0;
		if(mEditor.mAlarm.cycle.isAviableWeekSet())
		{
			if(mEditor.mAlarm.cycle.isWeekEveryDay())
			{
				pos = 1;
			}
			else
			{
				return;
			}
		}
		mEditor.mAlarm.cycle.setFlag(TimeCycle2.FLAG_NONE);
		mEditor.mAlarm.cycle.setCycle(getCycleByPosition(pos));
		mAdpater.setPosition(pos);
		mAdpater.notifyDataSetChanged();
		mEditor.mDigital.update(mEditor.mAlarm);
	}
	
	protected void initCycleRecord(TimeCycle2 cycle)
	{
		int type = cycle.getCycle();
		if(type == TimeCycle2.TYPE_HOUR)
		{
			mlFlagHour = cycle.getFlags();
		}
		else if(type == TimeCycle2.TYPE_WEEK)
		{
			mlFlagWeek = cycle.getFlags();
		}
	}
	
	protected void checkCycleFlags(TimeCycle2 cycle)
	{
		int type = cycle.getCycle();
		if(type != TimeCycle2.TYPE_HOUR && type != TimeCycle2.TYPE_WEEK)
		{
			cycle.setFlag(TimeCycle2.FLAG_NONE);
		}
	}
}
